package com.lnsf.bookstore.web.comment.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.IgnoreAuth;
import com.lnsf.bookstore.common.annotation.LoginUser;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.system.book.service.BookService;
import com.lnsf.bookstore.system.category.service.CategoryService;
import com.lnsf.bookstore.system.user.service.UserService;
import com.lnsf.bookstore.web.book.service.ApiBookService;
import com.lnsf.bookstore.web.comment.service.ApiCommentService;
import com.lnsf.bookstore.web.comment.vo.ApiComment;
import com.lnsf.bookstore.web.user.vo.ApiUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@RequestMapping(value = "/api/comment")
public class ApiCommentController extends ApiBaseAction{
	
	@Autowired
	private ApiCommentService apiCommentService;
	@Autowired
	private ApiBookService apiBookService;

	/**
	 * @param bookId
	 * @param page
	 * @param size
	 * @return
	 */
	@IgnoreAuth
	@ResponseBody
	@RequestMapping("list")
	public Object list(@LoginUser ApiUser apiUser) {
		Map<String, Object> params = new HashMap();
		Map<String, Object> resultMap = new HashMap();
		JSONObject jsonObject = getJsonRequest();
		params.put("bookId", jsonObject.getInteger("bookId"));
		params.put("page", jsonObject.getInteger("page"));
		params.put("limit", jsonObject.getInteger("size"));
		params.put("orderId", jsonObject.getInteger("orderId"));
		PageInfo<ApiComment> commentList = apiCommentService.getPage(params);
		resultMap.put("commentList",commentList);

		Map stars = apiCommentService.queryTotal(params);
		resultMap.put("commentCounts",stars);
		return toResponsSuccess(resultMap);
	}


	@ResponseBody
	@RequestMapping("post")
	public Object post(@LoginUser ApiUser apiUser) {
		JSONObject jsonObject = getJsonRequest();
		List<ApiComment>  list = (List<ApiComment>) jsonObject.get("commentList");
		List<ApiComment> apiCommentList = JSONObject.parseArray(list.toString(),ApiComment.class);

		for (ApiComment apiComment : apiCommentList){
			apiComment.setCreateTime(new Date());
			apiComment.setStatus(1);
			apiComment.setUserName(apiUser.getUsername());
			apiComment.setUserId(apiUser.getUserId());
		}
		apiCommentService.save(apiCommentList);
		return toResponsMsgSuccess("评论成功");
	}


}
